package com.mcptt.config.body.location;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParser;

import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.models.lbs.ReportData;
import com.cloudptt.api.models.lbs.LocationData;
import com.cloudptt.api.models.lbs.VitalData;
import com.cloudptt.api.product.log.Log;

import android.util.Xml;


public class LocationInfoBody_Distribute {


	private static final String THIS_FILE = "LocationInfoBody_New";
//	private static final String End_Mcptt_Tag = "</location-info>"; 
	
	private static final String Name_Report = "Report";
	private static final String Name_Data = "Date";
	private static final String Name_User = "Ur";
//	private static final String Name_Entry = "uri-entry";
//	private static final String Name_CurrentLocation = "Cu";
//	private static final String Name_TriggerId = "TriggerId";
//	private static final String Name_Latitude = "La";
//	private static final String Name_Longitude = "Lo";
	
//	private static final String Name_Vitalsign = "Vi";
//	private static final String Name_Work = "Wo";
//	private static final String Name_Heart = "He";
//	private static final String Name_Oxygen = "Oxygen";

	private boolean isLocTrigger = false;
	private List<ReportData> dataList = null;


	private LocationInfoBody_Distribute()
	{
		this.isLocTrigger = false;
		this.dataList = new Vector<ReportData>();
	}
	public LocationInfoBody_Distribute(boolean isLocTrigger,List<ReportData> dataList)
	{
		this.isLocTrigger = isLocTrigger;
		this.dataList = dataList;
	}

	public static LocationInfoBody_Distribute parseXmlStr(String xmlStr) throws Exception 
	{    
		LocationInfoBody_Distribute body = new LocationInfoBody_Distribute();

//		int index = xmlStr.indexOf(End_Mcptt_Tag);
		//	    Log.d(THIS_FILE, "McpttInfoBody_NewCall parseXmlStr index = " + index);

		//	    if(index <= 0)
		//	    {
		//	    	key = End_Mcvideo_Tag;
		//	    	index = xmlStr.indexOf(key);
		//	    }
//		xmlStr= xmlStr.substring(0, index + End_Mcptt_Tag.length());
		xmlStr = xmlStr.trim();
		
		ReportData data = null;
		long lDate = 0;

		ByteArrayInputStream strIs = new  ByteArrayInputStream(xmlStr.getBytes());
		XmlPullParser pullParser = Xml.newPullParser();
		pullParser.setInput(strIs, "UTF-8"); //为Pull解释器设置要解析的XML数据        
		int event = pullParser.getEventType();

		while (event != XmlPullParser.END_DOCUMENT){

			switch (event) {

			case XmlPullParser.START_DOCUMENT:         
				break;    
			case XmlPullParser.START_TAG:
				if (Name_Report.equals(pullParser.getName()))
				{
					String val = pullParser.getAttributeValue("", Name_Data);
					lDate = CheckData.parseLong(val);
					Log.i(THIS_FILE, "lDate = " + lDate);
				}
				else if (Name_User.equals(pullParser.getName()))
				{
					String dataStr = pullParser.nextText();
					Log.i(THIS_FILE, "dataStr = " + dataStr);
					data = new ReportData(lDate,dataStr);
					body.dataList.add(data);
				}
//				else if (Name_CurrentLocation.equals(pullParser.getName()))
//				{
//					locData = new LocationData();
//				}
//				else if (Name_Latitude.equals(pullParser.getName()))
//				{
//					String value = pullParser.nextText();
//					double val = CheckData.parseDouble(value);
//					locData.setLattitude(val);
//				}
//				else if (Name_Longitude.equals(pullParser.getName()))
//				{
//					String value = pullParser.nextText();
//					double val = CheckData.parseDouble(value);
//					locData.setLongitude(val);
//				}
//				else if (Name_Vitalsign.equals(pullParser.getName()))
//				{
//					vitalData = new VitalData();
//				}
//				else if (Name_Work.equals(pullParser.getName()))
//				{
//					String value = pullParser.nextText();
//					long val = CheckData.parseInt(value);
//					vitalData.setWorkHours(val);
//				}
//				else if (Name_Heart.equals(pullParser.getName()))
//				{
//					String value = pullParser.nextText();
//					int val = CheckData.parseInt(value);
//					vitalData.setHeartRate(val);
//				}
//				else if (Name_Oxygen.equals(pullParser.getName()))
//				{
//					String value = pullParser.nextText();
//					vitalData.setOxygenVolume(value);
//				}
				break;

			case XmlPullParser.END_TAG:
				if (Name_User.equals(pullParser.getName()))
				{
//					data.setLocData(locData);
//					data.setVitalData(vitalData);
//					body.dataList.add(data);
				}
				break;

			}

			event = pullParser.next();
		}

		return body;
	}

	public String toString()
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">");
		strBuff.append("<Report  data=\"12365488\" ReportType=\"NonEmergency\">");

		if(dataList != null)
		{
			for(Iterator<ReportData> itor = dataList.iterator();itor.hasNext();)
			{
				ReportData data = itor.next();
				String num = data.getNum();
				strBuff.append("<User uri-entry=\"");
				strBuff.append(num).append("\">");
				
				LocationData locData = data.getLocData();
				strBuff.append(locData.toString());
				
				VitalData vitalData = data.getVitalData();
				strBuff.append(vitalData.toString());
				
				strBuff.append("</User>");
			}
		}

		strBuff.append("</Report>");
		strBuff.append("</location-info>");

		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}
	
//	private String getLocationDataStr(LocationData data)
//	{
//		StringBuffer strBuff = new StringBuffer();
//		strBuff.append("<CurrentLocation>");
//		
//		strBuff.append("<Longitude type=\"Normal\">");
//		strBuff.append(data.getLongitude());
//		strBuff.append("</Longitude>");
//	
//		strBuff.append("<Latitude type=\"Normal\">");
//		strBuff.append(data.getLattitude());
//		strBuff.append("</Latitude>");
//		
//		strBuff.append("</CurrentLocation>");
//		
//		return strBuff.toString();
//	}


	public static String getTestStr(String mcpttId,boolean isLocTrigger,LocationData data)
	{
		StringBuffer strBuff = new StringBuffer();
		strBuff.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		strBuff.append("<location-info xmlns=\"urn:3gpp:ns:mcpttLocationInfo:1.0\">");
		strBuff.append("<Report ReportID=\"xxx\" ReportType=\"Emergency\">");
		if(isLocTrigger)
		{
			strBuff.append("<TriggerId>").append(mcpttId).append("</TriggerId>");
		}
		strBuff.append("<CurrentLocation>");
		strBuff.append("<CurrentCoordinate>");

		if(data != null)
		{
			strBuff.append("<Longitude type=\"Normal\">");
			strBuff.append(data.getLongitude());
			strBuff.append("</Longitude>");

			strBuff.append("<Latitude type=\"Normal\">");
			strBuff.append(data.getLattitude());
			strBuff.append("</Latitude>");
		}

		strBuff.append("</CurrentCoordinate>");
		strBuff.append("</CurrentLocation>");
		strBuff.append("</Report>");

		strBuff.append("</location-info>");

		try {
			return new String(strBuff.toString().getBytes(),"UTF-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return strBuff.toString();
	}

	public boolean isLocTrigger() {
		return isLocTrigger;
	}

	public List<ReportData> getDataList()
	{
		return dataList;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
